package com.jetsum.micro.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jetsum.micro.entity.Repo;
import com.jetsum.micro.mapper.RepoMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements RepoService {

    @Resource
    private RepoMapper repoMapper;

    @Override
    public String save() {
        Repo repo = new Repo();
        repo.setCount(1);
        repo.setCreatedTime(LocalDateTime.now());
        repo.setCreatedBy("test");
        int count = repoMapper.insert(repo);
        return "success";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String adjust(int count) {
        int i;
        LambdaUpdateWrapper<Repo> wrapper = new LambdaUpdateWrapper<>();
        if (count == 0)
            i = 1 / count;
        wrapper.eq(Repo::getId,1);
        wrapper.set(Repo::getCount,count);
        this.update(wrapper);
        return "success";
    }
}
